package com.ljf.tijian.service;

import com.ljf.tijian.ApiResponse;
import com.ljf.tijian.dto.CidetailedReportDto;
import com.ljf.tijian.dto.ReportConclusionDto;
import com.ljf.tijian.dto.ReportDto;
import com.ljf.tijian.mapper.CheckitemMapper;
import com.ljf.tijian.mapper.CidetailedreportMapper;
import com.ljf.tijian.mapper.OrdersMapper;
import com.ljf.tijian.mapper.OverallresultMapper;
import com.ljf.tijian.pojo.Checkitem;
import com.ljf.tijian.pojo.Cidetailedreport;
import com.ljf.tijian.pojo.Overallresult;
import com.ljf.tijian.service.Impl.ReportServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

@Service
public class ReportService implements ReportServiceImpl {

    @Autowired
    OrdersMapper ordersMapper;

    @Autowired
    OverallresultMapper overallresultMapper;

    @Autowired
    CidetailedreportMapper cidetailedreportMapper;
    
    @Autowired
    CheckitemMapper checkitemMapper;

    @Override
    public ApiResponse getAllReport(Long userId) {
        List<ReportDto> reportDtos = ordersMapper.selectAllReportsByUserId(userId);
        return new ApiResponse(200,"SUCCESS",reportDtos);
    }

    @Transactional
    @Override
    public ApiResponse getReportConclusion(Integer orderId) {

        ReportConclusionDto reportConclusion = new ReportConclusionDto();
        List<Cidetailedreport> cidetailedreports = cidetailedreportMapper.selectErrorData(orderId);
        List<Overallresult> overallresults = overallresultMapper.selectByOrderId(orderId);

        reportConclusion.setError_data(cidetailedreports);
        reportConclusion.setConclusion(overallresults);

        return new ApiResponse(200,"SUCCESS",reportConclusion);
    }

    @Transactional
    @Override
    public ApiResponse getReportDetail(Integer orderId) {

        List<Cidetailedreport> cidetailedreports = cidetailedreportMapper.selectReportDetailedData(orderId);
        List<Checkitem> checkitemList = checkitemMapper.selectAllCheckItem();
        List<CidetailedReportDto> list = new ArrayList<>();
        HashSet<Integer> set = new HashSet<>();
        for (Cidetailedreport cidetailedreport : cidetailedreports) {
            set.add(cidetailedreport.getCiId());
        }
        for (Integer i : set) {
            CidetailedReportDto cidetailedReportDto = new CidetailedReportDto();
            cidetailedReportDto.setCiId(i);
            cidetailedReportDto.setList(new ArrayList<>());
            list.add(cidetailedReportDto);
        }
        for (Cidetailedreport cidetailedreport : cidetailedreports) {
            for (CidetailedReportDto cidetailedReportDto : list) {
                if(cidetailedreport.getCiId()==cidetailedReportDto.getCiId()){
                    for (Checkitem checkitem : checkitemList) {
                        if(cidetailedReportDto.getCiId() == checkitem.getCiId()){
                            cidetailedReportDto.setName(checkitem.getCiName());
                            cidetailedReportDto.getList().add(cidetailedreport);
                        }
                    }
                }
            }
        }

        return new ApiResponse(200,"SUCCESS",list);
    }
}
